<?php

/**
 * This is the model class for table "member".
 *
 * The followings are the available columns in table 'member':
 * @property integer $_id
 * @property integer $insertTime
 * @property integer $updateTime
 * @property integer $ownerId
 * @property string $name
 * @property string $callSign
 * @property string $birthPlace
 * @property string $reference
 * @property string $gender
 */
class Member extends ActiveRecord {

	const GENDER_MAN = 'Laki-laki';
	const GENDER_WOMAN = 'Perempuan';

	/**
	 * Returns the static model of the specified AR class.
	 * @return Member the static model class
	 */
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'member';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ownerId', 'numerical'),
			array('name, callSign, birthPlace, gender', 'required'),
			array(
				'name, callSign, code, birthPlace, reference, gender',
				'length',
				'max'=>255
			),
			array(
				'callSign, code',
				'unique',
				'caseSensitive'=>false,
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'name, callSign, code, birthPlace, reference, gender',
				'safe',
				'on'=>'search'
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'dates'=>array(self::HAS_MANY, 'MemberDate', 'memberId'),
			'images'=>array(self::HAS_MANY, 'MemberImage', 'memberId'),
			'addresses'=>array(self::HAS_MANY, 'MemberAddress', 'memberId'),
			'phones'=>array(self::HAS_MANY, 'MemberPhone', 'memberId'),
			'photo'=>array(
				self::HAS_ONE,
				'MemberImage',
				'memberId',
				'condition'=>'name=:name',
				'params'=>array(':name'=>MemberImage::NAME_PHOTO),
			),
			'homeAddress'=>array(
				self::HAS_ONE,
				'MemberAddress',
				'memberId',
				'condition'=>'name=:name',
				'params'=>array(':name'=>MemberAddress::NAME_HOME),
			),
			'birthDate'=>array(
				self::HAS_ONE,
				'MemberDate',
				'memberId',
				'condition'=>'name=:name',
				'params'=>array(':name'=>MemberDate::NAME_BIRTH_DATE),
			),
			'expireDate'=>array(
				self::HAS_ONE,
				'MemberDate',
				'memberId',
				'condition'=>'name=:name',
				'params'=>array(':name'=>MemberDate::NAME_EXPIRE_DATE),
			),
			'senkomTitle'=>array(
				self::HAS_ONE,
				'MemberOrganization',
				'memberId',
				'condition'=>'name=:name',
				'params'=>array(':name'=>MemberOrganization::NAME_SENKOM),
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'name'=>'Nama',
			'callSign'=>'Call Sign',
			'code'=>'No Reg',
			'birthPlace'=>'Tempat Lahir',
			'reference'=>'Referensi',
			'gender'=>'Jenis Kelamin',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('name', $this->name, true);
		$criteria->compare('callSign', $this->callSign, true);
		$criteria->compare('code', $this->code, true);
		$criteria->compare('birthPlace', $this->birthPlace, true);
		$criteria->compare('reference', $this->reference, true);
		$criteria->compare('gender', $this->gender, true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

	public function addChild($child) {
		$child->ownerId = $this->ownerId;
		$child->memberId = $this->_id;
		return $child->save();
	}

	public function getGenderList() {
		return array(
			self::GENDER_MAN,
			self::GENDER_WOMAN,
		);
	}

	/**
	 * Add expire date.
	 */
	public function addExpireDate() {
		$model = new MemberDate;
		$model->name = MemberDate::NAME_EXPIRE_DATE;
		$expireDate = mktime(0, 0, 0, date('n'), date('j'), date('Y') + 3);
		$model->value = date('Y-m-d', $expireDate);
		return $this->addChild($model);
	}

	/**
	 * Add member to cardreq.
	 */
	public function addCardreq() {
		$model = new Cardreq;
		$criteria = new CDbCriteria;
		$criteria->compare('status', Cardreq::STATUS_DRAFT, false);
		$userId = Yii::app()->user->id;
		$criteria->compare('ownerId', $userId);
		$data = "$this->code <$this->name>";
		if ($model->exists($criteria)) {
			$model = $model->find($criteria);
			$data = trim($model->memberCodes) . "\r\n" . $data;
		}
		$model->ownerId = $userId;
		$model->status = Cardreq::STATUS_DRAFT;
		$model->memberCodes = $data;
		return $model->save(false);
	}
}